package cn.iocoder.yudao.module.wine.controller.admin.complaintype;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.complaintype.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.complaintype.ComplainTypeDO;
import cn.iocoder.yudao.module.wine.service.complaintype.ComplainTypeService;

@Tag(name = "管理后台 - 投诉信息类型")
@RestController
@RequestMapping("/wine/complain-type")
@Validated
public class ComplainTypeController {

    @Resource
    private ComplainTypeService complainTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建投诉信息类型")
    @PreAuthorize("@ss.hasPermission('wine:complain-type:create')")
    public CommonResult<Integer> createComplainType(@Valid @RequestBody ComplainTypeSaveReqVO createReqVO) {
        return success(complainTypeService.createComplainType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新投诉信息类型")
    @PreAuthorize("@ss.hasPermission('wine:complain-type:update')")
    public CommonResult<Boolean> updateComplainType(@Valid @RequestBody ComplainTypeSaveReqVO updateReqVO) {
        complainTypeService.updateComplainType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除投诉信息类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:complain-type:delete')")
    public CommonResult<Boolean> deleteComplainType(@RequestParam("id") Integer id) {
        complainTypeService.deleteComplainType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得投诉信息类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:complain-type:query')")
    public CommonResult<ComplainTypeRespVO> getComplainType(@RequestParam("id") Integer id) {
        ComplainTypeDO complainType = complainTypeService.getComplainType(id);
        return success(BeanUtils.toBean(complainType, ComplainTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得投诉信息类型分页")
    @PreAuthorize("@ss.hasPermission('wine:complain-type:query')")
    public CommonResult<PageResult<ComplainTypeRespVO>> getComplainTypePage(@Valid ComplainTypePageReqVO pageReqVO) {
        PageResult<ComplainTypeDO> pageResult = complainTypeService.getComplainTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ComplainTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出投诉信息类型 Excel")
    @PreAuthorize("@ss.hasPermission('wine:complain-type:export')")
    @OperateLog(type = EXPORT)
    public void exportComplainTypeExcel(@Valid ComplainTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ComplainTypeDO> list = complainTypeService.getComplainTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "投诉信息类型.xls", "数据", ComplainTypeRespVO.class,
                        BeanUtils.toBean(list, ComplainTypeRespVO.class));
    }

}